<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 表单组件 -->
      <el-card>
        <!-- 表头 -->
        <el-row type="flex" justify="space-between" class="top-box">
          <el-col>
            <el-button type="primary" @click="addAgent">添加经纪</el-button>
          </el-col>
          <el-row type="flex" justify="start" class="top-right">
            <el-input v-model="name_contains" placeholder="请输入内容" class="input-with-select">

              <el-button slot="append" icon="el-icon-search" @click="search" />
            </el-input>
          </el-row>
        </el-row>
        <hr style="border:1px solid #f5f7fa">
        <!-- 表格内容 -->
        <el-table :data="tableData" border style="width: 100%" class="top">
          <el-table-column prop="image" label="图片" width="150px">
            <template v-slot="{ row }">
              <img :src="row.image" alt="" style="width:150px; padding-right: 20px;">
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="邮箱" prop="email" />
          <el-table-column label="电话" prop="phone" />
          <el-table-column label="地址" prop="address" />
          <el-table-column label="介绍" prop="desc" />

          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button size="mini" type="primary" @click="edit(row.id)">编辑</el-button>
              <el-button size="mini" type="danger" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>

      <!-- 弹窗组件 -->
      <el-dialog
        :title="formData.id?'编辑经纪':'添加经纪'"
        :visible="dialogVisible"
        width="40%"
        @close="closeFn"
      >
        <!-- form表单 -->
        <el-form ref="ruleForm" label-width="80px" :model="formData" :rules="rules">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="formData.email" />
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="formData.phone" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="formData.address" />
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input v-model="formData.desc" type="textarea" style="width: 82%" />
          </el-form-item>
          <el-form-item label="封面" prop="image">
            <!-- <el-input v-model="formData.image" /> -->
            <el-upload class="avatar-uploader" action="" :on-change="handleAvatarSuccess">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-row type="flex" justify="center">
            <el-button @click="closeFn">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
          </el-row>
        </span>
      </el-dialog>

    </div>
  </div>
</template>
<script>
import { getAgentsList, addAgents, getAgentsById, editAgents, delAgents } from '@/api/agents'

export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      page: 0,
      total: 0,
      pageSize: 5,
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入经纪人名称', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请介绍经纪人', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }

        ]
      },
      imageUrl: '',
      name_contains: ''
    }
  },
  async created() {
    this.getAgents()
    // const res = await getAgentsList()
    // // console.log('129', res)
    // this.total = res.data.length
    this.getAgentsNumber() // 获取经纪人总数量
  },
  methods: {
    async getAgentsNumber() {
      const res = await getAgentsList()
      // console.log('129', res)
      this.total = res.data.length
      console.log(138, this.total)
    },
    async getAgents(val) {
      // console.log('117', val)
      if (val) {
        const result = await getAgentsList({

          name_contains: val
        })
        console.log('result', result)
        this.tableData = result.data
        this.total = result.data.length
      } else {
        const res = await getAgentsList({
          _limit: this.pageSize,
          _start: this.page
        // name_contains: val
        })
        // console.log('res', res)
        this.tableData = res.data
        // this.total = res.data.length
      }
    },
    // 添加经纪人
    async addAgent() {
      this.dialogVisible = true
    },
    // 确定按钮
    confirm() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          // console.log('132', this.formData)
          if (this.formData.id) {
            await editAgents(this.formData)
          } else {
            await addAgents(this.formData)
            console.log(this.total)
          }
          // 关闭弹窗
          // this.dialogVisible = false
          // this.$refs.ruleForm.resetFields()
          this.closeFn()
          // 重新渲染
          this.getAgents()
          this.getAgentsNumber()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // resetForm() {
    //   this.$refs.ruleForm.resetFields()
    // },
    // 切换分页
    pageChange(page) {
      // console.log(186, e)
      this.page = (page - 1) * this.pageSize
      this.getAgents()
      this.getAgentsNumber()
    },
    // 编辑按钮
    async edit(id) {
      const res = await getAgentsById(id)
      console.log(134, res)
      this.dialogVisible = true
      this.formData = res.data
      this.imageUrl = res.data.image
    },
    closeFn() {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
    },
    async search(value) {
      console.log('144', value)
      // this.name_contains = value
      await this.getAgents(this.name_contains)
    },
    // 图片上传
    handleAvatarSuccess(res, file) {
      // this.imageUrl = 'https://img1.baidu.com/it/u=105101192,4203910896&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375'
      this.imageUrl = 'https://img1.baidu.com/it/u=3340628873,4127536453&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313'
      // this.formData.image = 'https://img1.baidu.com/it/u=105101192,4203910896&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375'
      this.formData.image = 'https://img1.baidu.com/it/u=3340628873,4127536453&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313'
    },
    // 删除功能
    del(id) {
      // console.log(97, id)
      this.$confirm('确定删除该经纪人吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        // console.log('确定')
        // 删除
        await delAgents(id)
        this.getAgents()
        this.getAgentsNumber()
        // 更新列表，当删除最后一条数据的时候，需要请求上一页的数据
        if (this.tableData.length === 1 && this.page !== 0) {
          this.page = this.page - this.pageSize
        }
        // 更新列表
        this.getAgents()
      }).catch(err => {
        console.log('取消：', err)
      })
    }

  }

}

</script>
<style scoped lang="scss">
::v-deep .el-input__inner {
  width: 400px;
}
.biankuan {
  border-top: #dcdfe6;
}
.top{
    margin-top: 20px;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #ccc;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
 .el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>
